var statistics = require('../model/statistics'),
	nodeExcel = require('excel-export');

exports.selectStatis = function(req, res, connection) {
	var pageSize = req.query.pageSize;
	var levSta = req.query.levSta;
	var time = req.query.time;
	var cpage = req.query.cpage;
	var keyword = req.query.keyword;
	var begin = pageSize * (cpage - 1);
	var end = pageSize * cpage - 1;
	// console.log('key:', keyword);
	var rules = req.session.rule;
	// console.log("selectStatis--rules:", rules);
	statistics.selectByKey(connection, keyword, time, levSta, rules, function(rows) {
		var data = [];
		var k = 0;
		for (var i = begin; i < end + 1; i++) {
			if (i < rows.length) {
				data[k] = rows[i];
				k++;
			}
		}
		var sendData = {
			rows: rows,
			data: data,
			count: rows.length
		}
		res.send(sendData);
	});
};

exports.exportExcel = function(req, res, connection) {
	var conf = {};
	conf.cols = [{
		caption: '序号',
		type: 'number'
	}, {
		caption: '行政部门',
		type: 'string'
	}, {
		caption: '工程部门',
		type: 'string'
	}, {
		caption: '归属项目',
		type: 'string'
	}, {
		caption: '员工名称',
		type: 'string'
	}, {
		caption: '直接主管',
		type: 'string'
	}, {
		caption: '总分',
		type: 'number'
	}, {
		caption: '级别',
		type: 'string'
	}, {
		caption: '状态',
		type: 'string'
	}, {
		caption: '评价',
		type: 'string'
	}];

	var keyword = req.query.keyword;
	var time = req.query.time;
	var rules = req.session.rule;
	var levSta = req.query.levSta;

	statistics.selectByKey(connection, keyword, time,levSta, rules, function(rows) {
		var newData = [];
		// console.log(rows);
		var date = rows[0].perfDate;
		for (var i = 0; i < rows.length; i++) {
			if(rows[i].state=="0"){
				rows[i].state = "未审核";
			} else if (rows[i].state == "1") {
				rows[i].state = "未确认";
			} else if (rows[i].state == "2") {
				rows[i].state = "已确认";
			} else if (rows[i].state == "3") {
				rows[i].state = "退回重审";
			}
			newData.push([i + 1, rows[i].orgName, rows[i].projectOrg, rows[i].project, rows[i].employeeName, rows[i].superior, rows[i].score, rows[i].level, rows[i].state, rows[i].comment]);
		}
		conf.rows = newData;
		var result = nodeExcel.execute(conf);

		res.setHeader('Content-Type', 'application/vnd.openxmlformats');
		var filename = encodeURIComponent("恒拓开源员工月度绩效考核汇总");
		res.setHeader("Content-Disposition", "attachment; filename=" + filename + date + ".xlsx; filename *= utf-8" + filename + date + ".xlsx");

		res.end(result, 'binary');
	});
};

exports.statisPerf = function(req, res, connection) {
	var time = req.query.time;
	var rules = req.session.rule;
	// console.log("selUnWrite--rules:", rules);
	statistics.statisPerf(connection, time, rules, function(rows) {
		// console.log("selUnWrite--rows:",rows);
		res.send(rows);
	});

};
exports.selRatio = function(req, res, connection) {
	var time = req.query.time;
	var rules = req.session.rule;
	var sendResult = [];
	statistics.selARatio(connection, time, rules, function(rows) {
		if (rows != null && rows.length != 0) {
			for (var i = 0; i < rows.length; i++) {
				rowData = {
					deptId: rows[i].departmentId,
					deptName: rows[i].orgName,
					deptAddr: rows[i].orgAddress
				};
				var num = rows[i].num;
				var total = rows[i].total;
				// console.log(i +"---num=" + num + "--total=" + total);
				if (num == null || num == 0 || total == null || total == 0) {
					rowData.ratio = 0;
				} else {
					// console.log("ratio:", Math.round((num/total)*100)/100);
					rowData.ratio = Math.round((num / total) * 100);
				}
				sendResult.push(rowData);
			}

		}
		res.send(sendResult);
	});
};

exports.analyzePerf = function(req, res, connection) {
	var year = req.query.year;
	var type = req.query.type;
	var pageSize = req.query.pageSize;
	var cpage = req.query.cpage;
	var begin = pageSize * (cpage - 1);
	var end = pageSize * cpage - 1;

	statistics.analyzePerf(connection, year, type, function(rows) {

		var dataList = [];
		var i = rows.length - 1;
		if(rows != null) {
			exports.selMonths(connection, rows, dataList, i, year, type, function(data) {
				var analyzeData = [];
				var k = 0;
				if(data.length <= pageSize) {
					analyzeData = data;
				}else{
					for(var i = begin; i < end + 1; i++) {
						if(i < data.length) {
							analyzeData[k] = data[i];
							k++;
						}
					}
				}

				var sendData = {
					data: analyzeData,
					count: data.length
				};
				res.send(sendData);
			});
		}
		// res.send(rows);
	});
};
exports.selMonths = function(connection, rows, dataList, i, year, type, callback) {
	if(i >= 0) {
		var list = {};
		// list.rowData = rows[i];
		statistics.analyzeDetails(connection, rows[i].employeeId, year, type, function(result) {
			if(result.length == 2) {
				var month1 = result[0].perfDate.substr(-1);
				var month2 = result[1].perfDate.substr(-1);
				//判断是否为连续两个月都为A/D
				if(Math.abs(month1-month2) == 1) {
					list.dateList = result;
					list.rowData = rows[i];
					dataList.push(list);
				}
			}else{
				list.dateList = result;
				list.rowData = rows[i];
				dataList.push(list);
			}
			
			i--;
			exports. selMonths(connection, rows, dataList, i, year, type, function(data) {
				callback(data);
			});
		});

	}else{
		callback(dataList);
	}
};